package com.ormbench.base.action;

import javax.servlet.http.HttpServletRequest;

import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ormbench.base.LinkUtil;
import com.ormbench.base.StripesUtil;

public class BaseActionBeanContext extends ActionBeanContext {

	public static final String RESOLUTION_ERROR = "/vm/error.vm";

	private static final Log log = LogFactory.getLog(BaseActionBeanContext.class);

	public void addMessage(String message) {
		if (message != null && message.length() > 0) {
			this.getMessages().add(new SimpleMessage(message));
		}
	}

	@Override
	public Resolution getSourcePageResolution() {
		Resolution resolution = null;
		try {
			resolution = super.getSourcePageResolution();
		} catch (Exception e) {
			resolution = new RedirectResolution(RESOLUTION_ERROR);
			String url = this.getRequest().getRequestURI();
			log.error("Unable to determine source page resolution url: " + url, e);
		}
		return resolution;
	}

	// Set attributes available to all templates
	public void setRequest(HttpServletRequest request) {
		super.setRequest(request);
		request.setAttribute("stripes", new StripesUtil(request));
		request.setAttribute("link", new LinkUtil(request));
	}
}
